import { defineStore } from 'pinia'
import { ref } from 'vue'

type current_user = {
    avatar: string | null,
    email: string,
    id: number,
    is_admin: boolean,
    name: string,
    type: string
}

type playlist_info = {
    create_at?: string,
    folder_id: null | string,
    id: number,
    is_smart: boolean,
    name: string,
    rules: null | string | object,
    type: string,
    user_id: number,
    path?: string
}


export const useUserStore = defineStore(
    'user-store',
    () => {
        const userName = ref('')
        const is_Admin = ref(false)
        const Type = ref('')
        const Email = ref<string>('')
        const Avatar = ref<string | null>('')
        const playLists =ref<playlist_info[] | null | [] >(null)
        const userId = ref(1)

        const getUserInfo = async () => {


        }
        const setUserInfo = ({ name, is_admin, type, id, email, avatar } : current_user) => {
            userName.value = name
            is_Admin.value = is_admin
            Type.value = type
            Email.value = email
            Avatar.value = avatar
            userId.value = id

        }

        const setUserPlayList = (list: playlist_info[] | []) => {
            playLists.value = getUserPlayList(list)
        }

        const getUserPlayList = (list: playlist_info[] | [] ) => {
            if(list.length > 0)
            {
                return list.map((item)=>{
                    return {...item, path: '/playlist/' + item.id + '/' + item.name}
                })
            }
            else {
                return []
            }
        }

        return {
            setUserInfo,
            userName,
            is_Admin,
            Type,
            Email,
            Avatar,
            playLists,
            setUserPlayList,
            getUserPlayList
        }
    },
    {
        persist: false
    }
)
